package com.myweather.android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.myweather.android.R;
import com.myweather.android.adapter.TabPageAdapter;
import com.myweather.android.bean.MessageEnvent;
import com.myweather.android.fragment.FragmentTab1;

import java.util.ArrayList;

public class NewsActivity extends AppCompatActivity {

    private Toolbar mToobar;
    private DrawerLayout mDrawerLayout;
    private NavigationView mNavView;
    private TabLayout mTabLayout;
    private ViewPager mVpNews;
    private ArrayList<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        initView();
        setSupportActionBar(mToobar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.ic_daohang);
        }
        //        mNavView.setCheckedItem(R.id.nav_1);
        mNavView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.nav_1:
                        mDrawerLayout.closeDrawers();
                        startActivity(new Intent(NewsActivity.this, ContentActivity.class));

                        break;
                    case R.id.nav_2:
                        mDrawerLayout.closeDrawers();
                        startActivity(new Intent(NewsActivity.this, FeedbackActivity.class));

                        break;
                    case R.id.nav_3:
                        mDrawerLayout.closeDrawers();
                        Toast.makeText(NewsActivity.this, "清除成功", Toast.LENGTH_SHORT).show();

                        break;
                    case R.id.nav_4:
                        mDrawerLayout.closeDrawers();
                        finish();
                        startActivity(new Intent(NewsActivity.this, WeatherActivity.class));

                        break;
                }
                return true;
            }
        });

    }

    private void initView() {
        mToobar = (Toolbar) findViewById(R.id.toobar);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mNavView = (NavigationView) findViewById(R.id.nav_view);
        mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
        mVpNews = (ViewPager) findViewById(R.id.vp_news);

        fragments = new ArrayList<>();
        final FragmentTab1 fgt0 = new FragmentTab1();
        final FragmentTab1 fgt1 = new FragmentTab1();
        final FragmentTab1 fgt2 = new FragmentTab1();
        final FragmentTab1 fgt3 = new FragmentTab1();
        final FragmentTab1 fgt4 = new FragmentTab1();
        final FragmentTab1 fgt5 = new FragmentTab1();
        final FragmentTab1 fgt6 = new FragmentTab1();
        final FragmentTab1 fgt7 = new FragmentTab1();
        final FragmentTab1 fgt8 = new FragmentTab1();
        fragments.add(fgt0);
        fragments.add(fgt1);
        fragments.add(fgt2);
        fragments.add(fgt3);
        fragments.add(fgt4);
        fragments.add(fgt5);
        fragments.add(fgt6);
        fragments.add(fgt7);
        fragments.add(fgt8);
        String[] mTabNames = new String[]{"头条", "社会", "国内", "国际", "娱乐", "体育", "军事", "科技", "财经"};
        TabPageAdapter tabPageAdapter = new TabPageAdapter(getSupportFragmentManager(), fragments, mTabNames);
        mVpNews.setAdapter(tabPageAdapter);
        mTabLayout.setupWithViewPager(mVpNews);
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                MessageEnvent messageEnvent = new MessageEnvent(0);
                //tab项选中状态时执行
                switch (tab.getPosition()) {
                    case 0:
                        fgt0.upData("http://v.juhe.cn/toutiao/index?type=top&key=52a4b8a337f42dd376661f8b61c7ba6d");
                        //                        Toast.makeText(NewsActivity.this, "1", Toast.LENGTH_SHORT).show();

                        break;
                    case 1:
                        fgt1.upData("http://v.juhe.cn/toutiao/index?type=shehui&key=52a4b8a337f42dd376661f8b61c7ba6d");

                        break;
                    case 2:
                        fgt2.upData("http://v.juhe.cn/toutiao/index?type=guonei&key=52a4b8a337f42dd376661f8b61c7ba6d");

                        break;
                    case 3:
                        fgt3.upData("http://v.juhe.cn/toutiao/index?type=guoji&key=52a4b8a337f42dd376661f8b61c7ba6d");
                        break;
                    case 4:
                        fgt4.upData("http://v.juhe.cn/toutiao/index?type=yule&key=52a4b8a337f42dd376661f8b61c7ba6d");
                        break;
                    case 5:
                        fgt5.upData("http://v.juhe.cn/toutiao/index?type=tiyu&key=52a4b8a337f42dd376661f8b61c7ba6d");
                        break;
                    case 6:
                        fgt6.upData("http://v.juhe.cn/toutiao/index?type=junshi&key=52a4b8a337f42dd376661f8b61c7ba6d");
                        break;
                    case 7:
                        fgt7.upData("http://v.juhe.cn/toutiao/index?type=keji&key=52a4b8a337f42dd376661f8b61c7ba6d");
                        break;
                    case 8:
                        fgt8.upData("http://v.juhe.cn/toutiao/index?type=caijing&key=52a4b8a337f42dd376661f8b61c7ba6d");
                        break;

                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //tab项取消选中状态时执行

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //tab项选中状态再次点击时执行

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.backup:
                NewsActivity.this.finish();
                break;
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                break;
            default:

        }

        return true;
    }
}
